<?php
Class Works extends MY_Controller{

	function __construct()
	{
		parent::__construct();
		if(!$this->admin || $this->admin->status !=1 ) redirect('login');

	}


	function index()
	{
		if($_POST){
			if($_POST['action'] == 'up'){
				$neighbor = work::find_by_position($_POST['position']-1);
				$neighbor->position = $_POST['position'];
				$neighbor->save();
				$up = work::find_by_id($_POST['work_id']);
				$up->position = $_POST['position']-1;
				$up->save();
			}else{
				$neighbor = work::find_by_position($_POST['position']+1);
				$neighbor->position = $_POST['position'];
				$neighbor->save();
				$down = work::find_by_id($_POST['work_id']);
				$down->position = $_POST['position']+1;
				$down->save();
			}
			redirect('catalog/works');
		}
		$data['title'] = $this->title('Works');
		$data['works'] = work::find('all',array('order'=>'position asc'));
		$this->load->view($this->theme->admin_theme.'/header',$data);
		$this->load->view($this->theme->admin_theme.'/catalog/works/index');
		$this->load->view($this->theme->admin_theme.'/footer');
	}


	function viewWork($id = NULL)
	{
		if($id !=NULL && is_numeric($id)){

			if($_POST){
				if($_POST['action'] == 'up'){
					$neighbor = photo::find_by_position_and_work_id($_POST['position']-1,$id);
					$neighbor->position = $_POST['position'];
					$neighbor->save();
					$up = photo::find_by_id($_POST['id']);
					$up->position = $_POST['position']-1;
					$up->save();
				}elseif($_POST['action'] == 'down'){
					$neighbor = photo::find_by_position_and_work_id($_POST['position']+1,$id);
					$neighbor->position = $_POST['position'];
					$neighbor->save();
					$down = photo::find_by_id($_POST['id']);
					$down->position = $_POST['position']+1;
					$down->save();
				}else{
					$work = work::find_by_id($id);
					$work->photo_id = $_POST['id'];
					$work->save();
				}
				redirect('catalog/works/viewWork/'.$id);
			}

			$work = work::find($id);
			if($work->photo_id != NULL){
				$find_cover = photo::find_by_id($work->photo_id);
				if($find_cover){
					$data['cover'] = $find_cover;
				}else{
					$data['cover'] = NULL;
				}
			}else{
				$data['cover'] = NULL;
			}

			$photos = photo::find('all',array('conditions'=>array('work_id'=>$id),'order'=>'position asc'));
			$data['photos'] = $photos;
			$data['lastPosition'] = count($photos);
			$data['work'] =$work;
			$data['title'] = $this->title('Works');
			$this->load->view($this->theme->admin_theme.'/header',$data);
			$this->load->view($this->theme->admin_theme.'/catalog/works/viewWork');
			$this->load->view($this->theme->admin_theme.'/footer');

		}else{
			redirect('catalog/works');
		}
	}


	function addNewWork()
	{
		if($_POST){
			$item = work::find('all');
			$position = count($item)+1;
			if($_POST['customer_id']==0){
				$customer_id = NULL;
			}else{
				$customer_id = $_POST['customer_id'];
			}
			$data = array('id'=>'','name'=>$_POST['name'],'client_id'=>$customer_id,'active'=>$_POST['active'],'position'=>$position,'description'=>$_POST['description']);
			$newWork = work::create($data);
			if($newWork){

				redirect('catalog/works/viewWork/'.$newWork->id);
			}else{
				redirect('catalog/works');
			}
		}


		$data['customers'] = client::find('all');
		$data['title'] = $this->title('Works');
		$this->load->view($this->theme->admin_theme.'/header',$data);
		$this->load->view($this->theme->admin_theme.'/catalog/works/addNewWork');
		$this->load->view($this->theme->admin_theme.'/footer');
	}

	function edit($id = NULL)
	{

		if($id !=NULL && is_numeric($id)){
			if($_POST){
					
				if($_POST['customer_id']==0){
					$customer_id = NULL;
				}else{
					$customer_id = $_POST['customer_id'];
				}
		
				$item = work::find_by_id($id);
				$item->name = $_POST['name'];
				$item->client_id = $customer_id;
				$item->description = $_POST['description'];
				$item->active = $_POST['active'];
				if(isset($_POST['tag_id'])){
					$tag_array = implode(',',$_POST['tag_id']);
					$item->tag = $tag_array;
				}else{
					$item->tag = NULL;
				}
				if($item->save()){
					$data['msg'] = 'Update has been executed successfully';
				}
			}
			$work = work::find_by_id($id);
			if($work->tag !=NULL){
				$data['tagItems'] = explode(',',$work->tag);
			}
			$data['tags'] = tag::find('all');
			$data['editWork'] = $work;
			$data['customer'] = client::find('all');
			$data['title'] = $this->title('Work');
			$this->load->view($this->theme->admin_theme.'/header',$data);
			$this->load->view($this->theme->admin_theme.'/catalog/works/editWork');
			$this->load->view($this->theme->admin_theme.'/footer');

		}else{
			redirect('catalog/works');
		}
	}

	function deletePhoto()
	{
		if($_POST){

			$id =$_POST['id'];
			$deleteItem = photo::find_by_id($id);
			if($deleteItem->delete()){
				$reposition = photo::find('all',array('conditions'=>array('position >? AND work_id=? ',$deleteItem->position,$deleteItem->work_id)));
				if($reposition):
				foreach ($reposition as $res){
					$res->position = $res->position-1;
					$res->save();
				}
				endif;
					
				/* check existe files */
				$l = get_file_info('../uploads/large/'.$deleteItem->filename_l);
				$m = get_file_info('../uploads/medium/'. $deleteItem->filename_m);
				$s = get_file_info('../uploads/thumbs/'. $deleteItem->filename_s);
				if($l){
					unlink(FCPATH.'../uploads/large/'.$deleteItem->filename_l);
				}
				if($m){
					unlink(FCPATH.'../uploads/medium/'.$deleteItem->filename_m);
				}
				if($s){
					unlink(FCPATH.'../uploads/thumbs/'.$deleteItem->filename_s);
				}
				/* end of check existe files */

				echo json_encode(array('result'=>true));
			}else{
				echo json_encode(array('result'=>false));
			}

		}else{
			redirect('catalog/works');
		}
	}

	function deleteWork($id = NULL)
	{
		if($id !=NULL && is_numeric($id)){
			/* check deleteItem existe */
			$deleteItem = work::find_by_id($id);
			if($deleteItem){

				$data['deleteItem'] = $deleteItem;
				$data['title'] = $this->title('Work');
				$this->load->view($this->theme->admin_theme.'/header',$data);
				$this->load->view($this->theme->admin_theme.'/catalog/works/deleteWork');
				$this->load->view($this->theme->admin_theme.'/footer');

			}else{
				redirect('catalog/works');
			}


		}else{
			redirect('catalog/works');
		}
	}

	function doDelete($id = NULL)
	{
		if($id !=NULL && is_numeric($id)){
			/* check deleteItem existe */
			$deleteItem = work::find_by_id($id);
			if($deleteItem){
				if($deleteItem->delete()){
					$reposition = work::find('all',array('conditions'=>array('position >'.$deleteItem->position)));
					if($reposition):
					foreach ($reposition as $res){
						$res->position = $res->position-1;
						$res->save();
					}
					endif;

					/* check photo of works */
					$photos = photo::find('all',array('conditions'=>array('work_id'=>$deleteItem->id)));
					if($photos):
					$id_array = array();
					for($i=0;count($photos)>$i;$i++){
						$id_array[] = $photos[$i]->id;
					}
						
					photo::table()->delete(array('id'=>$id_array));

					foreach ($photos as $p){
							
						/* check existe files */
						$l = get_file_info('../uploads/large/'.$p->filename_l);
						$m = get_file_info('../uploads/medium/'. $p->filename_m);
						$s = get_file_info('../uploads/thumbs/'. $p->filename_s);
						if($l){
							unlink(FCPATH.'../uploads/large/'.$p->filename_l);
						}
						if($m){
							unlink(FCPATH.'../uploads/medium/'.$p->filename_m);
						}
						if($s){
							unlink(FCPATH.'../uploads/thumbs/'.$p->filename_s);
						}
						/* end of check existe files */
					}
					endif;

				}

				redirect('catalog/works');

			}else{
				redirect('catalog/works');
			}


		}else{
			redirect('catalog/works');
		}
	}

}